﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace EzyChoice
{
    public partial class SensitivityGraph : Form
    {
        SimpleStructure simple = new SimpleStructure();
        EzcItem Item = new EzcItem();
        List<Alternative> Alts = new List<Alternative>();
        Participate Par = new Participate();

        public SensitivityGraph()
        {
            InitializeComponent();
        }

        public void SetGraph(EzcItem item, Participate par, List<Alternative> alts)
        {
            try
            {
                Item = item;
                Alts = alts;
                Par = par;

                simple = CloneStructure(item);

                trView.Nodes.Clear();
                trView.Nodes.Add(simple);
                trView.ExpandAll();

                trView.SelectedNode = simple;

                lblPar.Text = "Sensitivity Chart from " + par.Name;
            }
            catch
            {
            }

            //ConstructGraph();
        }

        public SimpleStructure CloneStructure(EzcItem item)
        {

            double w = 0.0;
            SimpleStructure sim = new SimpleStructure();

            try
            {
                if (item.ParticipateWeight.ContainsKey(Par))
                {
                    w = item.ParticipateWeight[Par];
                }

                if ((item as Goal) != null)
                {
                    w = 1.0;
                }

                sim = new SimpleStructure(Alts, item, w);

                if (item.Nodes.Count > 0)
                {

                    foreach (EzcItem it in item.Nodes)
                    {
                        sim.Nodes.Add(CloneStructure(it));
                    }
                }
                else
                {
                    Criteria cri = item as Criteria;
                    double sum = 0.0;
                    if (cri != null)
                    {
                        foreach (Alternative a in Alts)
                        {
                            if (a.Rating.ContainsKey(cri) && a.Rating[cri].ContainsKey(Par) && sim.Score.ContainsKey(a))
                                sim.Score[a] = a.Rating[cri][Par].GetNormalizedWeight();
                            sum += sim.Score[a];
                        }

                        sum = sum > 0 ? sum : 1;

                        foreach (Alternative a in Alts)
                        {
                            if (sim.Score.ContainsKey(a))
                                sim.Score[a] = sim.Score[a] / sum;
                        }
                    }
                }

            }
            catch
            {
            }

            return sim;
        }

        private void SensitivityGraph_Load(object sender, EventArgs e)
        {

        }

        private void ConstructGraph()
        {
            //Alternative a = Alts[0];
            SimpleStructure sim = trView.SelectedNode as SimpleStructure;

            try
            {
                if (sim != null)
                {
                    double weight = sim.Weight;

                    sim.SetWeight(0);
                    simple.ConstructScore();

                    crtSensi.Series.Clear();

                    foreach (Alternative a in Alts)
                    {
                        double score = Math.Round(simple.Score[a],3);
                        Series r = new Series(a.Name);
                        r.ChartType = SeriesChartType.Line;
                        r.Points.Clear();
                        r.Legend = "Legend1";
                        r.ChartArea = "ChartArea1";
                        r.BorderWidth = 3;
                        r.Points.Add(new DataPoint(0, score));

                        crtSensi.Series.Add(r);
                    }



                    simple.RollBackWeight();
                    simple.ConstructScore();

                    int i = 0;

                    lstOrderAlt.Items.Clear();

                    foreach (Alternative a in Alts)
                    {
                        double score = Math.Round( simple.Score[a],3);
                        Series r = crtSensi.Series[i++];
                        r.Points.Add(new DataPoint(weight, score));
                        r.Points.Add(new DataPoint(1, score));

                        lstOrderAlt.Items.Add((1-score) + "|" + a.Name);

                    }

                    lstOrderAlt.Sorted = true;
                    lstOrderAlt.Sorted = false;
                    for (int j = 0; j < lstOrderAlt.Items.Count; j++)
                    {
                        lstOrderAlt.Items[j] = ((string)lstOrderAlt.Items[j]).Split(new char[] { '|' })[1];
                    }

                    crtSensi.ChartAreas[0].AxisX.MajorGrid.Interval = sim.Weight;

                    trackBar1.Value = int.Parse(Math.Round(sim.Weight * 1000).ToString());
                    lblSense.Text = sim.Weight.ToString();

                }
                else
                {

                    simple.ConstructScore();

                    crtSensi.Series.Clear();

                    lstOrderAlt.Items.Clear();

                    foreach (Alternative a in Alts)
                    {
                        double score = Math.Round( simple.Score[a],3);
                        Series r = new Series(a.Name);
                        r.ChartType = SeriesChartType.Line;
                        r.Points.Clear();
                        r.Legend = "Legend1";
                        r.ChartArea = "ChartArea1";
                        r.BorderWidth = 3;
                        r.Points.Add(new DataPoint(0, score));
                        r.Points.Add(new DataPoint(0.5, score));
                        r.Points.Add(new DataPoint(1.0, score));

                        crtSensi.Series.Add(r);

                        lstOrderAlt.Items.Add((1-score) + "|" + a.Name);

                    }

                    lstOrderAlt.Sorted = true;
                    lstOrderAlt.Sorted = false;
                    for (int j = 0; j < lstOrderAlt.Items.Count; j++)
                    {
                        lstOrderAlt.Items[j] = ((string)lstOrderAlt.Items[j]).Split(new char[] { '|' })[1];
                    }

                    trackBar1.Value = 100;
                    lblSense.Text = "1";
                    crtSensi.ChartAreas[0].AxisX.MajorGrid.Interval = 1;
                }

                crtSensi.Refresh();
            }
            catch
            {
            }
        }

        private void trView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                lblCri.Text = (e.Node as SimpleStructure) != null ? (e.Node as SimpleStructure).iName : "--- None ---";
                ConstructGraph();
            }
            catch
            {
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            try
            {
                lblSense.Text = (trackBar1.Value / 1000.0).ToString();

                SimpleStructure sim = trView.SelectedNode as SimpleStructure;
                if (sim != null)
                {
                    double ww = trackBar1.Value / 1000.0;

                    sim.SetWeight(ww);
                    simple.ConstructScore();

                    int i = 0;

                    lstOrderAlt.Items.Clear();

                    foreach (Alternative a in Alts)
                    {
                        double score = Math.Round( simple.Score[a],3);
                        Series r = crtSensi.Series[i++];
                        //if (ww >=0 && ww<=0.01)
                        //{
                        //    r.Points[0].SetValueY(score);
                        //}
                        r.Points[1].SetValueXY(ww, score);
                        r.Points[2].SetValueY(score);

                        lstOrderAlt.Items.Add((1-score) + "|" +  a.Name);

                    }

                    lstOrderAlt.Sorted = true;
                    lstOrderAlt.Sorted = false;
                    for (int j = 0; j < lstOrderAlt.Items.Count; j++ )
                    {
                        lstOrderAlt.Items[j] = ((string)lstOrderAlt.Items[j]).Split(new char[] { '|' })[1];
                    }

                    crtSensi.ChartAreas[0].AxisX.MajorGrid.Interval = sim.Weight;
                    crtSensi.Refresh();

                }
            }
            catch
            {
            }

        }
    }
}
